/****************************************************************************
 *
 *  ALi (Shanghai) Corporation, All Rights Reserved. 2008 Copyright (C)
 *
 *  File: win_imageslide.c
 *
 *  Description: Mediaplayer image slide UI.
 *
 *  History:
 *  Date            Author  		Version  Comment
 *  ====            ======  		=======  =======
 *  1.  
 *  2.  2008.3.17   Sharon Zhang 	         edit for mediaplayer 2.0
 *
 ****************************************************************************/
#include <sys_config.h>

#ifdef USB_MP_SUPPORT
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libmp/media_player_api.h>
#include <api/libmp/pe.h>
#include <api/libimagedec/imagedec.h>
#include <api/libosd/osd_lib.h>

#include "control.h"
#include "osdobjs_def.h"
#include "images.id"
#include "string.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_media.h"
/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER    g_win_imageslide;
//extern BOOL jpeg_decoder_task_not_over;
extern BITMAP imageslide_exit;
extern BITMAP imageslide_rotate;
extern TEXT_FIELD imageslide_idx;
extern TEXT_FIELD imageslide_user_para;
extern BITMAP imageslide_pause;
extern BITMAP imageslide_rotateicon;

 UINT32 timer_slide = 0;
extern TEXT_FIELD decode_txt_progress;
extern BITMAP decode_bmp_progress;

#ifdef IMG_2D_TO_3D
IMAGE_SET image_setting;
UINT8 image_setting_inited = 0;//whether image setting has inited 
#endif

/*******************************************************************************
*	Function decalare
*******************************************************************************/
static VACTION slide_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT slide_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

#define SLIDE_SH_IDX WSTL_NOSHOW_IDX
#define EXIT_TXT_IDX WSTL_TEXT_06
#define IDX_TXT_IDX WSTL_TEXT_07
#define ROT_BMP_IDX WSTL_NOSHOW_IDX
#ifndef SD_UI
#define W_L  124
#define W_T  68
#ifdef SUPPORT_CAS_A
#define W_W 362
#else
#define W_W 342
#endif
#define W_H  64
#define SLIDE_OFFSET 8

#define EXIT_L (W_L + SLIDE_OFFSET)
#define EXIT_T (W_T + SLIDE_OFFSET)
#define EXIT_W 52
#define EXIT_H  30

#define ROT_L EXIT_L + EXIT_W +SLIDE_OFFSET
#define ROT_T (EXIT_T)
#define ROT_W 56
#define ROT_H  30

#define IDX_L ROT_L + ROT_W +SLIDE_OFFSET
#define IDX_T (EXIT_T - 3)
#define IDX_W 214  //for more than 10000 pics, can't show all
#define IDX_H  36

#define PAUSE_L 924
#define PAUSE_T EXIT_T
#define PAUSE_W 72
#define PAUSE_H  64
#else
#define W_L  124
#define W_T  68
#define W_W 342
#define W_H  64
#define SLIDE_OFFSET 8

#define EXIT_L (W_L + SLIDE_OFFSET)
#define EXIT_T (W_T + SLIDE_OFFSET)
#define EXIT_W 52
#define EXIT_H  30

#define ROT_L EXIT_L + EXIT_W +SLIDE_OFFSET
#define ROT_T (EXIT_T)
#define ROT_W 56
#define ROT_H  30

#define IDX_L ROT_L + ROT_W +SLIDE_OFFSET
#define IDX_T (EXIT_T - 3)
#define IDX_W 214  //for more than 10000 pics, can't show all
#define IDX_H  36

#define PAUSE_L 500
#define PAUSE_T EXIT_T
#define PAUSE_W 72
#define PAUSE_H  64
#endif

/*******************************************************************************
*	Objects Macro
*******************************************************************************/
#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
	DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
	0,0,0,0,0, l,t,w,h, SLIDE_SH_IDX,SLIDE_SH_IDX,SLIDE_SH_IDX,SLIDE_SH_IDX,   \
	slide_keymap,slide_callback,  \
	nxtObj, focusID,0)

/*
#define LDEF_TXTEXIT(root,varTxt,nxtObj,l,t,w,h,resID)		\
	DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,C_FONT_1, \
	0,0,0,0,0, l,t,w,h, EXIT_TXT_IDX,EXIT_TXT_IDX,EXIT_TXT_IDX,EXIT_TXT_IDX,   \
	NULL,NULL,  \
	C_ALIGN_CENTER | C_ALIGN_VCENTER, 4,0,resID,NULL)
*/

#define LDEF_TXTIDX(root,varTxt,nxtObj,l,t,w,h,str)		\
	DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,C_FONT_1, \
	0,0,0,0,0, l,t,w,h, IDX_TXT_IDX,IDX_TXT_IDX,IDX_TXT_IDX,IDX_TXT_IDX,   \
	NULL,NULL,  \
	C_ALIGN_CENTER| C_ALIGN_VCENTER, 0,0,0,str)

#define LDEF_BMP(root,varbmp,nxtObj,l,t,w,h,bmpID)		\
    DEF_BITMAP(varbmp,&root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, ROT_BMP_IDX,ROT_BMP_IDX,ROT_BMP_IDX,ROT_BMP_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,bmpID)

/*******************************************************************************
*	Objects evaluate
*******************************************************************************/
LDEF_BMP(g_win_imageslide,imageslide_exit,&imageslide_rotate, \
	EXIT_L,EXIT_T,EXIT_W,EXIT_H,IM_MEDIA_PLAYER_PIC_06)
	
LDEF_BMP(g_win_imageslide,imageslide_rotate,&imageslide_idx,	\
	ROT_L,ROT_T,ROT_W,ROT_H,IM_MEDIAPLAY_EXIT)

LDEF_TXTIDX(g_win_imageslide,imageslide_idx,NULL, \
	IDX_L,IDX_T,IDX_W,IDX_H,display_strs[0])

LDEF_TXTIDX(g_win_imageslide,imageslide_user_para,NULL, \
	PAUSE_L - 180,PAUSE_T - 10, 190, PAUSE_H,display_strs[0])
 
LDEF_BMP(g_win_imageslide,imageslide_pause,NULL,PAUSE_L,PAUSE_T,PAUSE_W, PAUSE_H,IM_PAUSE)

LDEF_BMP(g_win_imageslide,imageslide_rotateicon,NULL,PAUSE_L,PAUSE_T,46, 46,IM_MEDIAPLAY_ROTATE)
 
LDEF_WIN(g_win_imageslide,&imageslide_exit,W_L,W_T,W_W, W_H, 0)

DEF_TEXTFIELD(decode_txt_progress,NULL,NULL,C_ATTR_ACTIVE,C_FONT_1, \
	0,0,0,0,0, \
	(OSD_MAX_WIDTH >> 1) - 20,(OSD_MAX_HEIGHT>>1),60,28, \
	IDX_TXT_IDX,IDX_TXT_IDX,IDX_TXT_IDX,IDX_TXT_IDX,   \
	NULL,NULL,  \
	C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,display_strs[1])

DEF_BITMAP(decode_bmp_progress,NULL,NULL,C_ATTR_ACTIVE,C_FONT_1, \
	0,0,0,0,0, \
	(OSD_MAX_WIDTH >> 1) - 36,(OSD_MAX_HEIGHT>>1),74,74, \
	IDX_TXT_IDX,IDX_TXT_IDX,IDX_TXT_IDX,IDX_TXT_IDX,   \
	NULL,NULL,  \
	C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0)

/*******************************************************************************
*	local variable
*******************************************************************************/
UINT8 image_rot_ang;
PlayListHandle cur_playlist_image;
UINT16 cur_image_idx;
UINT8 image_folderloop_flag;

ID imageslide_timer_id;
ID imageslide_proc_id;
static UINT8 proc_cnt = 0;

BOOL from_imagepreview = FALSE;
BOOL from_imagesetting = FALSE;
BOOL back_to_filelist = FALSE;
BOOL g_from_imageslide = FALSE;

#define IMG_PAUSE_STATE 1
#define IMG_UNPAUSE_STATE 0

#define IMAGE_ANGLE_CNT 4

static BOOL imageslide_pause_state = IMG_UNPAUSE_STATE;
static UINT8 user_para = 0;
extern IMAGE_SLIDE_INFO image_slide_setting;
UINT8 loading_progress;
static BOOL decode_finish_flag = FALSE;
extern FileListHandle cur_filelist;


UINT16 loading_bmp_ids[] = 
{
	IM_MEDIAPLAY_LOADING_01,
	IM_MEDIAPLAY_LOADING_02,
	IM_MEDIAPLAY_LOADING_03,
	IM_MEDIAPLAY_LOADING_04,
	IM_MEDIAPLAY_LOADING_05,
	IM_MEDIAPLAY_LOADING_06,
	IM_MEDIAPLAY_LOADING_07,
	IM_MEDIAPLAY_LOADING_08,
	IM_MEDIAPLAY_LOADING_09,
	IM_MEDIAPLAY_LOADING_10,
	IM_MEDIAPLAY_LOADING_11,
	IM_MEDIAPLAY_LOADING_12
};
#define LOADING_MAX  ( sizeof(loading_bmp_ids)/sizeof(loading_bmp_ids[0]) )

static PRESULT win_imageslide_unkown_act_proc(VACTION act);
static void win_imageslide_showexit(UINT8 flag);
static void win_imageslide_showrotate(UINT8 flag);
static void win_imageslide_showrotateicon(UINT8 flag);
static void win_imageslide_showidx(UINT8 flag);
void win_imageslide_show_user_para(UINT8 flag);
static void win_imageslide_showpause(UINT8 flag);
void win_imageslide_osd_onoff(UINT8 onoff);
static void win_imageslide_handler(UINT32 nouse);
static void win_imageslide_proc_handler(UINT32 nouse);

extern PlayListHandle create_play_list(PlayListType type, const char *name);

#define VACT_DECREASE (VACT_PASS +1)
#define VACT_INCREASE (VACT_PASS +2)

#define VACT_ROTATEL (VACT_PASS +3)
#define VACT_ROTATER (VACT_PASS +4)
#define VACT_PAUSE (VACT_PASS +5)
#define VACT_ZOOM (VACT_PASS +6)
#define VACT_MP (VACT_PASS +7)
#define VACT_PLAY (VACT_PASS + 8)
#ifdef IMG_2D_TO_3D
#define VACT_SWAP (VACT_PASS + 9)
#define VACT_INFO (VACT_PASS + 10)
#endif

/*******************************************************************************
*	Functions code
*******************************************************************************/
static VACTION slide_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;

	switch(key)
	{
	case V_KEY_LEFT:
		act = VACT_ROTATEL;
		break;
	case V_KEY_RIGHT:
		act = VACT_ROTATER;
		break;
	case V_KEY_UP:
		act = VACT_INCREASE;
		break;
	case V_KEY_DOWN:
		act = VACT_DECREASE;
		break;
	case V_KEY_PAUSE:
		act = VACT_PAUSE;
		break;
	case V_KEY_PLAY:
		act = VACT_PLAY;
		break;
	case V_KEY_ZOOM:
		act = VACT_ZOOM;
		break;
	case V_KEY_MP:
		act = VACT_MP;
		break;
	case V_KEY_EXIT:
		act = VACT_CLOSE;
		break;
	case V_KEY_MENU:
		if(from_imagepreview == TRUE)
			back_to_filelist = TRUE;
		act = VACT_CLOSE;
		break;
#ifdef IMG_2D_TO_3D
    case V_KEY_SWAP:
        act = VACT_SWAP;
        break;
    case V_KEY_INFOR:
        act = VACT_INFO;
        break;
#endif
	default:
		act = VACT_PASS;
	
	}

	return act;
}
static PRESULT slide_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT16 slide_interval;
	PlayListLoopType loop_type;
    struct osd_device *osd_dev;
    struct OSDRect pRect;
   	struct OSDPara OpenPara;    
    UINT32 region_id =0;

	VACTION unact;
	FileInfo file_node;
	char image_name[FULL_PATH_SIZE];
    char buf_tmp[40];
	int next_image;
	static UINT8 timer_start_flag = 0;
	UINT32 file_idx;

	switch(event)
	{
    case EVN_PRE_OPEN:
		imageslide_timer_id = OSAL_INVALID_ID;
		imageslide_proc_id = OSAL_INVALID_ID;

		if(from_imagepreview == TRUE)
		{
			imageslide_pause_state = IMG_PAUSE_STATE;
		}
		else
		{
			imageslide_pause_state = IMG_UNPAUSE_STATE;
		}
#ifdef IMG_2D_TO_3D
        if(from_imagesetting == TRUE)
		{
			imageslide_pause_state = IMG_PAUSE_STATE;
            from_imagesetting = FALSE;
		}
        
        if(0==image_setting_inited)
        {
        	image_setting.image_osd_onoff = IMAGE_SET_ON;
        	image_setting.image_3d_onoff = IMAGE_SET_OFF;
        	image_setting.image_3d_output_format = IMAGE_SET_3DOUTPUT_RB;
            image_setting_inited = 1;
        }
        win_imageslide_osd_onoff(image_setting.image_osd_onoff);
#endif
		win_imageslide_showpause(TRUE);
		api_set_preview_vpo_color(FALSE);
		break;
    case EVN_POST_OPEN:
		image_rot_ang = ANG_ORI;
		
		loop_type = image_slide_setting.bRepeat ? PLAY_LIST_REPEAT : PlAY_LIST_SEQUENCE;
		set_play_list_info(cur_playlist_image,&cur_image_idx,&loop_type);

		get_file_from_play_list(cur_playlist_image, cur_image_idx, &file_node);
		win_get_fullname(image_name, file_node.path, file_node.name);
//        while(jpeg_decoder_task_not_over)
//        {
//            libc_printf("error condition!\n");
//            osal_task_sleep(20);
//        }
        image_abort();
        timer_slide = osal_get_tick();
#ifdef IMG_2D_TO_3D        
        image_set_disp_param(image_setting.image_3d_onoff, image_setting.image_3d_output_format, 60, 20, 60);
#endif
        win_image_file_play(image_name, image_rot_ang);
        win_imageslide_showexit(TRUE);
        win_imageslide_showrotate(TRUE);
		win_imageslide_showidx(TRUE);
	    win_imageslide_show_user_para(TRUE);
		decode_finish_flag = FALSE;        
        break;
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		ret = win_imageslide_unkown_act_proc(unact);
		break;
	case EVN_PRE_CLOSE:
		g_from_imageslide = TRUE;
		imagedec_ioctl(1, IMAGEDEC_IO_CMD_CLEAN_FRM, TRUE);	//when quit win_slide, clean 2 frm buffer
		
		if(image_folderloop_flag == 1)//filelist
		{
			get_file_from_play_list(cur_playlist_image, cur_image_idx, &file_node);
			win_get_fullname(image_name, file_node.path, file_node.name);
			usblist_set_filelist_param(image_name);
		}
		image_abort();
///*
#ifdef  DUAL_ENABLE
#ifdef  PNG_GIF_TEST  

        pRect.uLeft =136;
	    pRect.uTop = 40;
	    pRect.uWidth = 1008;//OSD_MAX_WIDTH;//608;
	    pRect.uHeight = 640;//OSD_MAX_HEIGHT;//430;	
    
        OpenPara.eMode = OSD_HD_ARGB1555;
        OpenPara.uGAlpha = 0x0f;
        OpenPara.uGAlphaEnable = 0;
        OpenPara.uPalletteSel = 0;
        osd_dev = (struct osd_device *)dev_get_by_id(HLD_DEV_TYPE_OSD, 0);
        if (osd_dev != NULL)
	    {		
            OSDDrv_Close((HANDLE)osd_dev);
            //AD_GIF_PRINTF("%s() OSD_layer2 is closed!\n", __FUNCTION__);
        }
        OSDDrv_Open((HANDLE)osd_dev, &OpenPara);
        osal_task_sleep(20);
        //trans_color = OSD_GetTransColor(OpenPara.eMode,TRUE);
	    OSDDrv_IoCtl((HANDLE)osd_dev, OSD_IO_SET_TRANS_COLOR, 0x00);
	    OSDDrv_CreateRegion((HANDLE)osd_dev, region_id, &pRect, NULL);
        pRect.uLeft = pRect.uTop = 0;
        OSDDrv_RegionFill((HANDLE)osd_dev,region_id,&pRect,0x00);   
        OSDDrv_ShowOnOff((HANDLE)osd_dev, TRUE); 
            
#endif            
#endif
		api_stop_timer(&imageslide_timer_id);
		api_stop_timer(&imageslide_proc_id);
        timer_start_flag = 0;
		imageslide_timer_id = OSAL_INVALID_ID;
		imageslide_proc_id = OSAL_INVALID_ID;
		win_set_image_multiview_firstidx();

		OSD_ClearObject((POBJECT_HEAD)&imageslide_idx,C_UPDATE_ALL);
        //imageslide_user_para;
        OSD_ClearObject((POBJECT_HEAD)&imageslide_user_para,C_UPDATE_ALL);
            
		OSD_ClearObject((POBJECT_HEAD)&imageslide_pause,C_UPDATE_ALL);
		OSD_ClearObject((POBJECT_HEAD)&decode_txt_progress,C_UPDATE_ALL);
		OSD_ClearObject((POBJECT_HEAD)&decode_bmp_progress,C_UPDATE_ALL);
		break;
	case EVN_POST_CLOSE:
		image_restore_vpo_rect();//TODO
		break;
	case EVN_MSG_GOT:
        if(param1 == CTRL_MSG_SUBTYPE_CMD_STO && param2 == USB_STATUS_OVER)
        {
			//storage_dev_mount_hint(1);
			file_list_check_storage_device(FALSE, FALSE);
        }
		else if(param1 == CTRL_MSG_SUBTYPE_STATUS_IMAGEOVER)
		{
            if(param2 > timer_slide)
            {
    			image_abort();
            }
            else
            {
                libc_printf("slide @@@@@@@@@@@@@@ es case\n");
                break;
            }
			decode_finish_flag = TRUE;
			if(imageslide_pause_state == IMG_UNPAUSE_STATE)
		    {
				slide_interval = (image_slide_setting.uTime_gap * 1000);
				if(0 != slide_interval)
					imageslide_timer_id = api_start_timer("I",slide_interval,win_imageslide_handler);
				else
				{
					imageslide_pause_state = IMG_PAUSE_STATE;
					win_imageslide_showpause(TRUE);
				}
			}
		}
		else if (param1 == CTRL_MSG_SUBTYPE_STATUS_IMGTOOVER) // timer timeouts
		{
			//TODO: play next
			
//            while(jpeg_decoder_task_not_over)
//            {
//                libc_printf("\n%%%%%%%%%%%%%%%%%%%%%%%%%\n");
//                osal_task_sleep(20);
//            }
			image_abort();
			next_image = get_next_index_from_play_list(cur_playlist_image);
			OSD_ClearObject((POBJECT_HEAD)&imageslide_user_para,C_UPDATE_ALL);
			if (next_image != -1)
			{
				cur_image_idx = next_image;
				set_play_list_info(cur_playlist_image,&cur_image_idx,NULL);
				get_file_from_play_list(cur_playlist_image, cur_image_idx, &file_node);
				win_get_fullname(image_name, file_node.path, file_node.name);
#ifdef IMG_2D_TO_3D                
                image_set_disp_param(image_setting.image_3d_onoff, image_setting.image_3d_output_format, 60, 20, 60);
#endif
                win_image_file_play(image_name, image_rot_ang);
				decode_finish_flag = FALSE;
				ap_send_msg(CTRL_MSG_SUBTYPE_CMD_UPDATE_VIEW, 0, TRUE);
			}
			else
			{
				ap_send_msg(CTRL_MSG_SUBTYPE_CMD_EXIT, 0,TRUE);
			}
		}
		else if(param1 == CTRL_MSG_SUBTYPE_CMD_EXIT)
		{
			if(from_imagepreview == TRUE)
				back_to_filelist = TRUE;
			ret = PROC_LEAVE;
		}
		else if(param1 == CTRL_MSG_SUBTYPE_CMD_UPDATE_VIEW)
		{
			win_imageslide_showidx(TRUE);
			win_imageslide_show_user_para(TRUE);
			ret = PROC_LOOP;
		}
		else if(param1 == CTRL_MSG_SUBTYPE_STATUS_MP3OVER)
		{
			win_play_next_music_ex();
		}
		else if(param1 == CTRL_MSG_SUBTYPE_STATUS_PROC_UPDATE)
		{
			OSD_SetBitmapContent(&decode_bmp_progress, loading_bmp_ids[proc_cnt]);
			OSD_DrawObject((POBJECT_HEAD)&decode_bmp_progress,C_UPDATE_ALL);
			proc_cnt ++;
			if(proc_cnt >= LOADING_MAX)
				proc_cnt = 0;
		}		
		
		if ((param1 == CTRL_MSG_SUBTYPE_STATUS_JPEG_DEC_PROCESS) || (param1 == CTRL_MSG_SUBTYPE_STATUS_IMAGEOVER))
		{
			if ((param2 < 100) && (param1 == CTRL_MSG_SUBTYPE_STATUS_JPEG_DEC_PROCESS))
			{
				loading_progress = (param2 * LOADING_MAX)/100;
				if (loading_progress > LOADING_MAX - 1)
					loading_progress = LOADING_MAX - 1;

				if(0 == timer_start_flag)
				{
					imageslide_proc_id = OSAL_INVALID_ID;
					imageslide_proc_id = api_start_cycletimer("S", 200, win_imageslide_proc_handler);
					timer_start_flag = 1;
				}
			//	OSD_SetBitmapContent(&decode_bmp_progress, loading_bmp_ids[loading_progress]);
			//	OSD_DrawObject((POBJECT_HEAD)&decode_bmp_progress,C_UPDATE_ALL);
			}
			else if((param2 >= 100) || (param1 == CTRL_MSG_SUBTYPE_STATUS_IMAGEOVER))
			{
				OSD_ClearObject((POBJECT_HEAD)&decode_bmp_progress,C_UPDATE_ALL);
				api_stop_timer(&imageslide_proc_id);
				imageslide_proc_id = OSAL_INVALID_ID;
				timer_start_flag = 0;
				proc_cnt = 0;
			//	imageslide_proc_id = OSAL_INVALID_ID;
			}
		}
		break;
	default:
		break;
	}

	return ret;
}



static PRESULT win_imageslide_unkown_act_proc(VACTION act)
{
	PRESULT ret = PROC_PASS;
	UINT8 rotate_mode, display_mode;
	POBJECT_HEAD win,mplayer_menu;
	int next_image;
	UINT16 slide_interval;
	FileInfo file_node;
	char image_name[FULL_PATH_SIZE];
    char buf_tmp[40];
    UINT8 back_saved;
    SYSTEM_DATA *p_sys_data;
    p_sys_data = sys_data_get();

	switch(act)
	{
	case VACT_ROTATEL:
	case VACT_ROTATER:
//        image_abort();
		if(FALSE == decode_finish_flag)
			break;
#ifdef IMG_2D_TO_3D
		if(IMAGE_SET_ON==image_setting.image_3d_onoff)
			break;
#endif
		
		if(imageslide_pause_state == IMG_UNPAUSE_STATE)
		{
			api_stop_timer(&imageslide_timer_id);
			imageslide_pause_state = IMG_PAUSE_STATE;
		}
		if(act == VACT_ROTATER)
		{
			image_rot_ang = (image_rot_ang + 1)%IMAGE_ANGLE_CNT;
		}
		else
		{
			image_rot_ang = (image_rot_ang+IMAGE_ANGLE_CNT - 1)%IMAGE_ANGLE_CNT;
		}
		
		image_rotate(image_rot_ang);
		
		if(image_rot_ang == ANG_ORI)
		{
			win_imageslide_showrotateicon(FALSE);
			win_imageslide_showpause(TRUE);
		}
		else
		{
			OSD_ClearObject((POBJECT_HEAD)&imageslide_pause, 0);
			win_imageslide_showrotateicon(TRUE);
		}
		win_imageslide_showidx(TRUE);
		break;
	case VACT_INCREASE:
	case VACT_DECREASE:
//        image_abort();
		if(imageslide_pause_state == IMG_UNPAUSE_STATE)
		{
			api_stop_timer(&imageslide_timer_id);
			imageslide_pause_state = IMG_PAUSE_STATE;
			win_imageslide_showpause(TRUE);
		}
		if(act == VACT_INCREASE)
		{
			next_image = get_next_index_from_play_list(cur_playlist_image);
		}
		else
		{
			next_image = get_previous_index_from_play_list(cur_playlist_image);
		}
		if(next_image != -1)
		{
			cur_image_idx = next_image;
			api_stop_timer(&imageslide_timer_id);
			image_abort();
			if(image_rot_ang!= ANG_ORI)
			{
				image_rot_ang = ANG_ORI;
				win_imageslide_showrotateicon(FALSE);
				win_imageslide_showpause(TRUE);
			}
			osal_task_sleep(20);            // to end last decoder task
			set_play_list_info(cur_playlist_image,&cur_image_idx,NULL);
			get_file_from_play_list(cur_playlist_image, cur_image_idx, &file_node);
			win_get_fullname(image_name, file_node.path, file_node.name);
#ifdef IMG_2D_TO_3D                            
            image_set_disp_param(image_setting.image_3d_onoff, image_setting.image_3d_output_format, 60, 20, 60);
#endif
            win_image_file_play(image_name, image_rot_ang);
			decode_finish_flag = FALSE;
			osal_task_sleep(20);            // to run current decoder task
			win_imageslide_showidx(TRUE);
			win_imageslide_show_user_para(TRUE);
		}
		break;
	case VACT_PAUSE:
		if(imageslide_pause_state == IMG_UNPAUSE_STATE)
		{
			imageslide_pause_state = IMG_PAUSE_STATE;
			win_imageslide_showpause(TRUE);
			api_stop_timer(&imageslide_timer_id);
		}
	    else
	    {
			if(image_rot_ang == ANG_ORI)
			{
				imageslide_pause_state = IMG_UNPAUSE_STATE;
				slide_interval = (image_slide_setting.uTime_gap * 1000);
				win_imageslide_showpause(TRUE);
                
                image_abort();
				if(0 != image_slide_setting.uTime_gap)
					imageslide_timer_id = api_start_timer("I",slide_interval,win_imageslide_handler);
			}
	    }
		ret = PROC_LOOP;
		break;
	case VACT_PLAY:
		if(IMG_PAUSE_STATE == imageslide_pause_state)
		{
			if(image_rot_ang == ANG_ORI)
			{
				imageslide_pause_state = IMG_UNPAUSE_STATE;
				win_imageslide_showpause(TRUE);
			}
            else
            {
				imageslide_pause_state = IMG_UNPAUSE_STATE;
				win_imageslide_showrotateicon(FALSE); 
            }
            slide_interval = (image_slide_setting.uTime_gap * 1000);
			image_abort();
            if(0 != image_slide_setting.uTime_gap)
                imageslide_timer_id = api_start_timer("I",slide_interval,win_imageslide_handler);
		}
		ret = PROC_LOOP;
		break;
	case VACT_ZOOM:
//        image_abort();
#ifdef IMG_2D_TO_3D
		if(decode_finish_flag && (IMAGE_SET_ON!=image_setting.image_3d_onoff))
#else
		if(decode_finish_flag)
#endif
		{
			if(imageslide_pause_state == IMG_UNPAUSE_STATE)
			{
				imageslide_pause_state = IMG_PAUSE_STATE;
				win_imageslide_showpause(TRUE);
				api_stop_timer(&imageslide_timer_id);
			}

			win_mpzoom_open();

			if(image_rot_ang == ANG_ORI)
			{
				imageslide_pause_state = IMG_UNPAUSE_STATE;
				slide_interval = (image_slide_setting.uTime_gap * 1000);
				win_imageslide_showpause(TRUE);
				imageslide_timer_id = api_start_timer("I",slide_interval,win_imageslide_handler);
			}
		}
		break;
	case VACT_MP:
#ifdef IMG_2D_TO_3D
        if(IMAGE_SET_ON==image_setting.image_3d_onoff)
            break;
#endif        
		win = (POBJECT_HEAD)(&g_win_imageslide);
		OSD_ObjClose(win,C_CLOSE_CLRBACK_FLG);

		while((POBJECT_HEAD)&g_win_usb_filelist != menu_stack_get_top())
		{
			menu_stack_pop();
		}
		
		image_abort();
		win_set_image_multiview_firstidx();
		mplayer_menu = (POBJECT_HEAD)(&g_win_imagepreview);

		if(OSD_ObjOpen(mplayer_menu,(UINT32)(~0)) != PROC_LEAVE)
			menu_stack_push(mplayer_menu);
		break;
#ifdef IMG_2D_TO_3D
    case VACT_SWAP:
//        image_abort();
		if(decode_finish_flag && (image_rot_ang == ANG_ORI))
		{
            if(IMAGE_SET_ON==image_setting.image_3d_onoff)
            {
                image_setting.image_3d_onoff = IMAGE_SET_OFF;
            }
            else
            {
                image_setting.image_3d_onoff = IMAGE_SET_ON;
            }
			win_imageslide_show_user_para(TRUE);
            imageslide_pause_state = IMG_PAUSE_STATE;
            win_imageslide_showpause(TRUE);
            api_stop_timer(&imageslide_timer_id);

            if(IMAGE_SET_ON==image_setting.image_3d_onoff)
            {
                win_compopup_init(WIN_POPUP_TYPE_SMSG);
        		win_compopup_set_frame(200, 200, 200, 100);
        		win_compopup_set_msg(NULL, NULL,RS_DISPLAY_PLEASE_WAIT);
        		win_compopup_open_ext(&back_saved);
            }
            image_set_disp_param(image_setting.image_3d_onoff, image_setting.image_3d_output_format, 60, 20, 60);
            image_2d_to_3d_swap(image_setting.image_3d_onoff);
            if(IMAGE_SET_ON==image_setting.image_3d_onoff)
            {
                osal_task_sleep(50);
        		win_compopup_smsg_restoreback();
                sys_data_save(1);
            }
            
		    ret = PROC_LOOP;			
		}
		break;   
    case VACT_INFO:
//        image_abort();
		if(decode_finish_flag && (image_rot_ang == ANG_ORI))
		{
            imageslide_pause_state = IMG_PAUSE_STATE;
    		win_imageslide_showpause(TRUE);
    		api_stop_timer(&imageslide_timer_id);	
            if(OSD_ObjOpen((POBJECT_HEAD)&win_imageset_con, 0) != PROC_LEAVE)
			{
				menu_stack_push((POBJECT_HEAD) &win_imageset_con);
			}
            
		    ret = PROC_LOOP;	
	    }
		break;   
#endif
	default:
		break;
	}

	return ret;
}

static void win_imageslide_showexit(UINT8 flag)
{
    SYSTEM_DATA* p_sys_data = sys_data_get();
    
#ifdef IMG_2D_TO_3D
	if(image_setting.image_osd_onoff && flag)
#else
	if(flag)
#endif
	{
   		OSD_DrawObject((POBJECT_HEAD)&imageslide_exit,C_UPDATE_ALL);
	}
	else
	{
		OSD_ClearObject((POBJECT_HEAD)&imageslide_exit, 0);
	}
	return;
}

static void win_imageslide_showrotate(UINT8 flag)
{
	SYSTEM_DATA* p_sys_data = sys_data_get();
    
#ifdef IMG_2D_TO_3D
	if(image_setting.image_osd_onoff && flag)
#else
	if(flag)
#endif
	{
   		OSD_DrawObject((POBJECT_HEAD)&imageslide_rotate,C_UPDATE_ALL);
	}
    else
    {
		OSD_ClearObject((POBJECT_HEAD)&imageslide_rotate, 0);
    }

    return;
}

static void win_imageslide_showrotateicon(UINT8 flag)
{
	SYSTEM_DATA* p_sys_data = sys_data_get();
    
#ifdef IMG_2D_TO_3D
	if(image_setting.image_osd_onoff && flag)
#else
	if(flag)
#endif
	{
   		OSD_DrawObject((POBJECT_HEAD)&imageslide_rotateicon,C_UPDATE_ALL);
	}
    else
    {
		OSD_ClearObject((POBJECT_HEAD)&imageslide_rotateicon, 0);
    }

    return;
}


static void win_imageslide_showidx(UINT8 flag)
{
	char str_buffer[20];
	TEXT_FIELD *pTxt = &imageslide_idx;
	UINT32 image_total_num;
    SYSTEM_DATA* p_sys_data = sys_data_get();
    
	get_play_list_info(cur_playlist_image,&image_total_num, NULL);
	sprintf(str_buffer,"%d/%d",cur_image_idx,image_total_num);
	OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)str_buffer);

#ifdef IMG_2D_TO_3D
	if(image_setting.image_osd_onoff && flag)
#else
	if(flag)
#endif
		OSD_DrawObject((POBJECT_HEAD)pTxt,C_UPDATE_ALL);
    else
        OSD_ClearObject((POBJECT_HEAD)pTxt, 0);

    return;
}

void win_imageslide_show_user_para(UINT8 flag)
{
#ifdef IMG_2D_TO_3D
	TEXT_FIELD *pTxt = &imageslide_user_para;
    UINT8 str_buffer[128];
    
	SYSTEM_DATA* p_sys_data = sys_data_get();

    if(image_setting.image_osd_onoff && flag)
    {
        if(IMAGE_SET_ON==image_setting.image_3d_onoff)
        {
            sprintf(str_buffer,"3D");  
        }
        else
        {
            sprintf(str_buffer,"2D"); 
        }
            
    	OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)str_buffer);
    	OSD_DrawObject((POBJECT_HEAD)pTxt,C_UPDATE_ALL);
    }
    else
        OSD_ClearObject((POBJECT_HEAD)pTxt, 0);
#endif
    return;
}

static void win_imageslide_showpause(UINT8 flag)
{
    UINT16 slide_interval;

    SYSTEM_DATA* p_sys_data = sys_data_get();
    
#ifdef IMG_2D_TO_3D
	if(image_setting.image_osd_onoff && flag)
#else
	if(flag)
#endif
    {
        slide_interval = (image_slide_setting.uTime_gap * 1000);
        if(0 != slide_interval)
        {
    	    if(imageslide_pause_state == IMG_PAUSE_STATE)
    	    {
       	    	OSD_DrawObject((POBJECT_HEAD)&imageslide_pause,C_UPDATE_ALL);
    	    }
            else
            {
    	    	OSD_ClearObject((POBJECT_HEAD)&imageslide_pause, 0);
            }
        }
    }

    else
        OSD_ClearObject((POBJECT_HEAD)&imageslide_pause, 0);

    return;

}

void win_imageslide_osd_onoff(UINT8 onoff)
{  
    if(onoff)
    {
        OSD_SetAttr(&imageslide_exit, C_ATTR_ACTIVE);
        OSD_SetAttr(&imageslide_rotate, C_ATTR_ACTIVE);
        OSD_SetAttr(&imageslide_idx, C_ATTR_ACTIVE);
        OSD_SetAttr(&imageslide_user_para, C_ATTR_ACTIVE);
        OSD_SetAttr(&imageslide_pause, C_ATTR_ACTIVE);
        OSD_SetAttr(&decode_bmp_progress, C_ATTR_ACTIVE);
    }
    else
    {
        OSD_SetAttr(&imageslide_exit, C_ATTR_HIDDEN);
        OSD_SetAttr(&imageslide_rotate, C_ATTR_HIDDEN);
        OSD_SetAttr(&imageslide_idx, C_ATTR_HIDDEN);
        OSD_SetAttr(&imageslide_user_para, C_ATTR_HIDDEN);
        OSD_SetAttr(&imageslide_pause, C_ATTR_HIDDEN);
        OSD_SetAttr(&decode_bmp_progress, C_ATTR_HIDDEN);
    }
    
    win_imageslide_showexit(onoff);
    win_imageslide_showrotate(onoff);
    win_imageslide_showidx(onoff);
    win_imageslide_showpause(onoff);
    win_imageslide_show_user_para(onoff);


    return;
}


static void win_imageslide_handler(UINT32 nouse)
{
	if(imageslide_timer_id != OSAL_INVALID_ID)
	{
		api_stop_timer(&imageslide_timer_id);
		imageslide_timer_id = OSAL_INVALID_ID;
		ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_IMGTOOVER, 0, TRUE);
	}
}

static void win_imageslide_proc_handler(UINT32 nouse)
{
	if(imageslide_proc_id != OSAL_INVALID_ID)
		ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_PROC_UPDATE, 0, TRUE);

//	api_stop_timer(&imageslide_proc_id);
//	imageslide_proc_id = OSAL_INVALID_ID;
}


RET_CODE win_set_imageplay_param(PlayListHandle *pplaylist, UINT32 idx, UINT8 folder_flag)
{
	UINT8 back_saved;
	RET_CODE retcode = RET_SUCCESS;
	FileInfo filenode;
	char image_name[FULL_PATH_SIZE];
	int playlist_idx;

	image_folderloop_flag = folder_flag;
	if(image_folderloop_flag == 1)
	{
		if(*pplaylist != NULL)
		{
			if(RET_SUCCESS != delete_play_list(*pplaylist))
			{
				return RET_FAILURE;
			}
		}
		#ifndef _USE_32M_MEM_
		*pplaylist = create_play_list(IMAGE_PLAY_LIST, "folder_image");
		#else
		*pplaylist = create_play_list(IMAGE_PLAY_LIST, " ");
		#endif
		if(*pplaylist == NULL)
		{
			return RET_FAILURE;
		}
		
		get_file_from_file_list(cur_filelist, idx, &filenode);
		win_get_fullname(image_name, filenode.path, filenode.name);
		win_compopup_init(WIN_POPUP_TYPE_SMSG);
		win_compopup_set_frame(200, 200, 200, 100);
		win_compopup_set_msg(NULL, NULL,RS_DISPLAY_PLEASE_WAIT);
		win_compopup_open_ext(&back_saved);
		retcode = add_all_file_list_items_to_play_list(*pplaylist, cur_filelist);
		osal_task_sleep(500);
		win_compopup_smsg_restoreback();
		if(retcode != RET_SUCCESS)
		{
			return RET_FAILURE;
		}
		if((filenode.filetype == F_JPG) || (filenode.filetype == F_BMP))
		{
			playlist_idx = get_play_list_index_by_name(*pplaylist, image_name);
		}
		else
		{
			playlist_idx = 1;
		}
		if(playlist_idx == -1)
		{
			return RET_FAILURE;
		}
		cur_playlist_image = *pplaylist;
		cur_image_idx = playlist_idx;
	}
	else
	{
		cur_playlist_image = *pplaylist;
		cur_image_idx = idx;
	}
	set_play_list_info(cur_playlist_image, &cur_image_idx, NULL);

	return RET_SUCCESS;
}

void win_set_image_slide_idx(UINT32 idx)
{
	cur_image_idx = idx;
}

#endif

